import { defineStore } from "pinia";
import { getAllChapterAPI } from "../api/section.ts";
import { Chapter } from "../index";

const useSectionStore = defineStore("section", {
  persist: false,
  state: () => {
    return {
      chapters: [] as Chapter[],
      routeURL: "/",
    };
  },
  getters: {
    allChapter: (state) => {
      return state.chapters;
    },
  },
  actions: {
    async getAllChapter() {
      let response = await getAllChapterAPI();
      if (response.status !== 200) {
        throw new Error("网络错误");
      }
      let result: any = await response.data;
      const code = result.code;
      const data = result.data;
      if (code !== 0) {
        return false;
      }
      this.chapters = data;
      return true;
    },
    setRouteURL(url: string) {
      this.routeURL = url;
    },
  },
});

export default useSectionStore;
